<template>
  <div>
    <div id="app" class="wrapper wrapper-content">
      <div class="row">
          <div>
              <div class="panel panel-primary">
                  <div class="panel-body" style="padding: 0px 120px; box-sizing: border-box;">
                      <el-form size="small" :model="inputForm" ref="inputForm" v-loading="loading" :disabled="formReadOnly">
                          <div  style="text-align:center">
                              <h3 style="font-weight: bold;color: black;font-size:24px">资质使用申请单</h3>
                          </div>
                          <table style="margin:0 auto;width:80%;margin-top:20px;border-collapse: collapse;margin-bottom:20px">
                              <tr style="line-height: 3.5;">
                                  <td colspan="4"
                                      style="font-weight: bold;text-align: left;padding-left: 32px;position: relative;font-size: 14px">
                                      <img src="../../../../../assets/imgs/processManagement/work.png" alt=""
                                          style="position: absolute;width: 20px;height: 20px;left: 0px;top: 14px;margin-left: 8px">
                                      基本信息
                                  </td>
                              </tr>
                              <tr class="iconHidden">
                                  <td class="width-15">申请人</td>
                                  <td class="width-30" >
                                      <el-form-item  prop="applyuser.id" >
                                            <user-select :limit='1' :value="inputForm.applyuser.id" @getValue='(value) => {inputForm.applyuser.id=value}' disabled></user-select>
                                      </el-form-item> 
                                  </td>
                                  <td class="width-15" >所属公司</td>
                                  <td class="width-30"  >
                                      <el-form-item  prop="company.id">
                                      <SelectTree
                                                  ref="company"
                                                  :props="{
                                                      value: 'id',             // ID字段名
                                                      label: 'name',         // 显示名称
                                                      children: 'children'    // 子级字段名
                                                    }"
                                                  disabled
                                                  url="/sys/office/treeData?type=2"
                                                  :value="inputForm.company.id"
                                                  :clearable="true"
                                                  :accordion="true"
                                                  @getValue="(value) => {inputForm.company.id=value}"/>
                                      </el-form-item>
                                  </td>
                              </tr>
                              <tr  class="iconHidden">
                                  <td class="width-15">申请日期</td>
                                  <td class="width-30">
                                      <el-form-item prop="applytime" >
                                            <el-date-picker
                                                  v-model="inputForm.applydate"
                                                  type="datetime"
                                                  style="width: 100%;"
                                                  disabled
                                                  value-format="yyyy-MM-dd HH:mm:ss"
                                                  :clearable="false"
                                                  placeholder="请选择">
                                                </el-date-picker>
                                      </el-form-item>
                                  </td>
                                  <td class="width-15">所属部门</td>
                                  <td class="width-30">
                                      <el-form-item  prop="depart.id">
                                      <SelectTree
                                                  ref="depart"
                                                  :props="{
                                                      value: 'id',             // ID字段名
                                                      label: 'name',         // 显示名称
                                                      children: 'children'    // 子级字段名
                                                    }"
                                                  disabled
                                                  url="/sys/office/treeData?type=2"
                                                  :value="inputForm.depart.id"
                                                  :clearable="true"
                                                  :accordion="true"
                                                  @getValue="(value) => {inputForm.depart.id=value}"/>
                                      </el-form-item>   
                                  </td>
                              </tr>
                              <tr style="line-height: 3.5;">
                                  <td colspan="4"
                                      style="font-weight: bold;text-align: left;padding-left: 32px;position: relative;font-size: 14px">
                                      <img src="../../../../../assets/imgs/processManagement/basic.png" alt=""
                                          style="position: absolute;width: 20px;height: 20px;left: 0px;top: 14px;margin-left: 8px">
                                      借用信息
                                  </td>
                              </tr>
                              <tr class="iconHidden">
                                  <td class="width-15"><span style="color: red">*&nbsp;</span>借用日期</td>
                                  <td class="width-30">
                                      <el-form-item  prop="borrydate" :rules="[{required: true, message:'借用日期不能为空', trigger:'blur'}]">
                                            <el-date-picker
                                                  v-model="inputForm.borrydate"
                                                  type="date"
                                                  style="width: 100%;"
                                                  value-format="yyyy-MM-dd"                                               
                                                  format="yyyy-MM-dd"
                                                  @change="timeCondition"
                                                  placeholder="请选择">
                                                </el-date-picker>
                                      </el-form-item>
                                  </td>
                                  <td class="width-15"><span style="color: red">*&nbsp;</span>归还日期</td>
                                  <td class="width-30">
                                      <el-form-item  prop="backdate" :rules="[{required: true, message:'归还日期不能为空', trigger:'blur'}]">
                                            <el-date-picker
                                                  v-model="inputForm.backdate"
                                                  type="date"
                                                  style="width: 100%;"                                               
                                                  value-format="yyyy-MM-dd"
                                                  format="yyyy-MM-dd"
                                                  @change="timeCondition"
                                                  placeholder="请选择">
                                                </el-date-picker>
                                      </el-form-item>
                                  </td>
                              </tr>
                              <tr class="iconHiddens errorMsg">
                                  <td class="width-15"><span style="color: red">*&nbsp;</span>资质</td>
                                  <td class="width-30" >
                                      <el-form-item  prop="qualifications.id"  :rules="[{required: true, message:'资质不能为空', trigger:['blur','change']}]">
                                        <GridSelect
                                          title="选择资质"
                                          labelName = 'name'
                                          labelValue = 'id'
                                          :value = "inputForm.qualifications.id"                                      
                                          :limit="1"
                                          @getValue='(value) => {inputForm.qualifications.id=value}'
                                          :columns="[
                                          {
                                            prop: 'company.id',
                                            label: '所属公司'
                                          },
                                          {
                                            prop: 'name',
                                            label: '资质名称'
                                          },
                                          {
                                            prop: 'name',
                                            label: '到期时间'
                                          },
                                          {
                                            prop: 'enddate',
                                            label: '图片'
                                          }
                                          ]"
                                          :searchs="[
                                          {
                                            prop: 'company.id',
                                            label: '所属公司'
                                          },
                                          {
                                            prop: 'name',
                                            label: '资质名称'
                                          },
                                          {
                                            prop: 'name',
                                            label: '到期时间'
                                          }
                                          ]"
                                          dataListUrl="/dlyrl/oa/qualifications/oaQualifications/list"
                                          entityBeanName="oaQualifications"
                                          queryEntityUrl="/dlyrl/oa/qualifications/oaQualifications/queryById">
                                        </GridSelect>
                                      </el-form-item>
                                  </td>
                                  <td class="width-15"><span style="color: red">*&nbsp;</span>是否外带</td>
                                  <td class="width-30">
                                      <el-form-item  prop="isout" :rules="[{required: true, message:'是否外带不能为空', trigger:'blur'}]">
                                            <el-select v-model="inputForm.isout" placeholder="请选择"  style="width: 100%;">
                                              <el-option
                                                v-for="item in $dictUtils.getDictList('outtype')"
                                                :key="item.value"                                             
                                                :label="item.label"
                                                :value="item.value">
                                              </el-option>
                                          </el-select>
                                      </el-form-item>
                                  </td>
                              </tr>
                              <tr class="iconHiddens">
                                  <td class="width-15"><span style="color: red">*&nbsp;</span>资质类型</td>
                                  <td class="width-30">
                                      <el-form-item  prop="qualificationstype" :rules="[{required: true, message:'资质类型不能为空', trigger:'blur'}]">
                                          <el-select v-model="inputForm.qualificationstype" placeholder="请选择"  style="width: 100%;">
                                              <el-option
                                                v-for="item in $dictUtils.getDictList('qualificationstype')"
                                                :key="item.value"                                             
                                                :label="item.label"
                                                :value="item.value">
                                              </el-option>
                                          </el-select>
                                      </el-form-item>
                                  </td>
                                  <td class="width-15"><span style="color: red">*&nbsp;</span>使用事由</td>
                                  <td class="width-30">
                                    <el-input v-model="inputForm.content"  ></el-input>
                                  </td>
                              </tr>
                              <tr class="iconHidden">
                                  <td class="width-15">附件</td>
                                  <td colspan="4" style="position:relative;">
                                      <!-- :on-preview="handlePreview" -->
                                      <el-upload ref="fujian" 
                                        :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl/oa/applyqualifications/oaApplyQualifications`"
                                        :headers="{token: $cookie.get('token')}"
                                        :on-preview="(file) => {$window.open ((file.response && file.response.url) || file.url,'_blank') }"
                                        :on-success="(response, file, fileList) => {
                                          inputForm.fujian = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                                        }"
                                        :on-remove="(file, fileList) => {
                                          $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                                            $message.success(data.msg)
                                          })
                                          inputForm.fujian = fileList.map(item => item.url).join('|')
                                        }"
                                        :before-remove="(file, fileList) => {
                                          return $confirm(`确定移除 ${file.name}？`)
                                        }"
                                        multiple
                                        :limit="5"
                                        :on-exceed="(files, fileList) =>{
                                          $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                                        }"
                                        :file-list="fujianArra">
                                        <el-button size="small" type="primary" style="position:absolute;right:5px;top:4px;padding:9px"><i class="fa fa-cloud-upload" ></i></el-button>
                                      </el-upload>
                                  </td>
                              </tr>
                              <tr style="height: 80px;line-height: 80px">
                                  <td class="width-15 active" style="line-height: 80px">备注信息
                                  </td>
                                  <td colspan="4" style="padding:0 10px">
                                    <el-form-item  prop="remarks">
                                        <el-input v-model="inputForm.remarks" ></el-input>
                                    </el-form-item>
                                  </td>
                              </tr>
                          </table>
                      </el-form>                
                  </div>
              </div>
          </div>
      </div>
    </div>
  </div>
</template>
<script>
  import UserSelect from '@/components/userSelect'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import GridSelect from '@/components/gridSelect'
  import tableStyles from '../../../../../utils/mixins'
  export default {
    mixins: [tableStyles],
    data () {
      return {
        title: '',
        method: '',
        loading: false,
        fujianArra: [],
        inputForm: {
          id: '',
          applyuser: {
            id: ''
          },
          company: {
            id: ''
          },
          applydate: '',
          depart: {
            id: ''
          },
          borrydate: '',
          backdate: '',
          qualifications: {
            id: ''
          },
          isout: '',
          qualificationstype: '',
          content: '',
          fujian: '',
          remarks: ''
        },
      }
    },
    props: {
      businessId: {
        type: String,
        default: ''
      },
      formReadOnly: {
        type: Boolean,
        default: false
      }
    },
    components: {
      UserSelect,
      SelectTree,
      GridSelect
    },
    watch: {
      'businessId': {
        handler (newVal) {
          if (this.businessId) {
            this.init(this.businessId)
          } else {
            this.$nextTick(() => {
              this.$refs.inputForm.resetFields();
              if(this.$store.state.user.id!=''){
                var users=this.$store.state.user
              }else{
                var users=JSON.parse(sessionStorage.getItem("userInfo"));
              }
              this.inputForm.applyuser.id=users.id;
              this.inputForm.company.id=users.company.id;
              this.inputForm.depart.id=users.office.id;
              this.inputForm.applydate=this.getNowTime();
            })
          }
        },
        immediate: true,
        deep: false
      }
    },
    created(){
      window.addEventListener("beforeunload",()=>{
        sessionStorage.setItem("userInfo",JSON.stringify(this.$store.state.user))
      })
    },
    methods: {
      //时间选择的判断条件
      timeCondition(){
        if(this.inputForm.borrydate.length!=0&&this.inputForm.backdate.length!=0){
          var startDate=new Date(this.inputForm.borrydate);
          var endDate=new Date(this.inputForm.backdate);
          if(startDate>endDate){
            this.$message.warning('借用日期不能大于归还日期');
            this.inputForm.backdate='';
            return false
          }  
        }
      },
      init (id) {
        this.fujianArra = []
        if (id) {
          this.loading = true
          this.inputForm.id = id
          this.$nextTick(() => {
            this.$refs.inputForm.resetFields()
            this.$http({
              url: `/dlyrl/oa/applyqualifications/oaApplyQualifications/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.oaApplyQualifications)
              this.inputForm.fujian.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.fujianArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false;
            })
          })
        }
      },
      //回显文件点击下载功能
      handlePreview(file) {
        // console.log(file.url);
        document.location.href = file.url
      },
      // 表单提交
      saveForm (callback) {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/dlyrl/oa/applyqualifications/oaApplyQualifications/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                callback(data.businessTable, data.businessId);
                this.loading = false;
                this.$refs.inputForm.resetFields();
                this.inputForm.applyuser.id=this.$store.state.user.id;
                this.inputForm.company.id=this.$store.state.user.company.id;
                this.inputForm.depart.id=this.$store.state.user.office.id;
                this.inputForm.applydate= this.getNowTime();
                this.fujianArra=[];
              }
            })
          }
        })
      }
    }
  }
</script>